<?php
namespace App\Http\Service;

use Crypt;
use Illuminate\Http\Request;
use App\Http\Service\CommentService;
use App\Models\Admin;
use App\Models\AdminRole;

class AdministratorService
{
	private static $_obj;
	private $_adminModel;
	private $_comment;

	public static function getInstance()
	{
		if (!self::$_obj instanceof self) {
			self::$_obj = new self();
		}
		return self::$_obj;
	}

	public function __construct()
	{
		$this->_comment = CommentService::getInstance();
		$this->_adminModel = Admin::getInstance();
	}

	public function getAdministratorList()
	{
		$list['list'] = Admin::select('id','number','level','status','create_time','update_time')->get()->toArray();
		$list['count'] = count($list['list']);
		return $list;
	}

	/**
	 * 管理员添加操作
	 */
	public function addToAdministrator(Request $data)
	{
		try{
            $password = Crypt::encrypt($data->password);
        } catch(EncryptException $e) {
            return $this->_comment->returnArr(500, '管理员密码解析错误,请联系超管');
        }
        $level = $this->_comment->getSessionData()['level']+1;
		$admin = new Admin;
		$admin->number = $data->number;
        $admin->password = $password;
        $admin->level = $level;
        $admin->status = $data->status;
        $admin->tel = $data->tel;
        $admin->email = $data->email;
        $admin->remarks = $data->remarks;
        $admin->create_time = time();
        $aid = $admin->save();
        if ($aid) {
        	if ($data->rid && is_array($data->rid)) {
        		$res = AdminRole::getInstance()->addAdminRole($data->rid, $aid);
        		if ($res) {
        			return $this->_comment->returnArr(200, '添加成功');	
        		} else {
        			return $this->_comment->returnArr(200, '添加成功,节点/权限添加失败');
        		}
        	}
        	return $this->_comment->returnArr(200, '添加成功,未传入权限');
        } else {
        	return $this->_comment->returnArr(300, '添加失败');
        }
	}

	/**
	 * 获取单个管理员信息
	 */
	public function getAdministratorInformation($id)
	{
		$res = $this->_adminModel->getAdministratorDataField($id);
		$res['adminrole'] = AdminRole::getInstance()->getRoleListByAid($id);
		return $res;
	}

	public function delete($id)
	{
		$res = Admin::find($id);
		$level = $this->_comment->getSessionData()['level'];
		if (!empty($res)) {
			if ($level < $res->level) {
				$result = $res->delete();
				if ($result) {
					return $this->_comment->returnArr(200, '删除成功');
				} else {
					return $this->_comment->returnArr(300, '删除失败');
				}
			} else {
				return $this->_comment->returnArr(301, '对不起,您的权限还不够');
			}
		} else {
			return $this->_comment->returnArr(404, '管理员不存在?');
		}
	}

	/**
	 * 管理员编辑操作
	 */
	public function editToAdministrator(Request $data, $id)
	{
		if ($id==1) {
			return $this->_comment->returnArr(500, '您正在试图修改超级管理员信息');
		}
		$admin = Admin::find($id);
		$level = $this->_comment->getSessionData()['level'];
		if (!empty($admin)) {
			if ($level >= $admin->level) {
				return $this->_comment->returnArr(301, '对不起,您的权限还不够');
			}
		} else {
			return $this->_comment->returnArr(404, '管理员不存在?');
		}
		if ($data->password) {
			try{
	            $admin->password = Crypt::encrypt($data->password);
	        } catch(EncryptException $e) {
	            return $this->_comment->returnArr(500, '管理员密码解析错误,请联系超管');
	        }
		}
		if ($data->level) {
			$admin->level = $data->level;
		}
        $admin->status = $data->status;
        $admin->tel = $data->tel;
        $admin->email = $data->email;
        $admin->remarks = $data->remarks;
        $admin->update_time = time();
        $result = $admin->save();
        if ($result) {
        	if ($data->rid && is_array($data->rid)) {
        		AdminRole::getInstance()->delAdminRoleByAid($aid);  //话不多说,先删除关联表
        		$res = AdminRole::getInstance()->addAdminRole($data->rid, $aid);
        		if ($res) {
        			return $this->_comment->returnArr(200, '添加成功');	
        		} else {
        			return $this->_comment->returnArr(200, '添加成功,节点/权限添加失败');
        		}
        	}
        	return $this->_comment->returnArr(200, '编辑成功');
        } else {
        	return $this->_comment->returnArr(300, '编辑失败');
        }
	}

	/**
	 * 管理员更改状态
	 * int id 要修改的管理员唯一标识
	 * @author 朱旭
	 */
	public function status($id)
	{
		$data = $this->_adminModel->getAdministratorData($id);
		$level = $this->_comment->getSessionData()['level'];
		if (!empty($data)) {
			if ($level >= $data['level']) {
				return $this->_comment->returnArr(301, "权限不足");
			}
		}else{
			return $this->_comment->returnArr(404, '您要操作的管理不存在');
		}
		$status = $data['status']==1?2:1;
		$result = $this->_adminModel->saveAdministratorStatus($id, $status);
		if ($result) {
			return $this->_comment->returnArr(200, '更改成功');
		} else {
			return $this->_comment->returnArr(300, '更改失败');
		}
	}
}